package com.agiantii.ablog.mapper;

import com.agiantii.ablog.pojo.Post;
import com.agiantii.ablog.pojo.vo.PostVO;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface PostMapper {
    
    @Insert("INSERT INTO post(user_id, title, content, cover) VALUES(#{userId}, #{title}, #{content}, #{cover})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Post post);
    
    @Update("UPDATE post SET title = #{title}, content = #{content}, cover = #{cover}, " +
            "updated_at = CURRENT_TIMESTAMP WHERE id = #{id} AND deleted = 0")
    int update(PostVO post);
    
    @Update("UPDATE post SET deleted = 1 WHERE id = #{id}")
    int deleteById(Long id);
    
    @Select("SELECT p.*, u.username FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "WHERE p.id = #{id} AND p.deleted = 0 ORDER BY p.created_at DESC")
    PostVO findById(Long id);
    
    @Select("SELECT p.*, u.username FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "WHERE p.user_id = #{userId} AND p.deleted = 0 ORDER BY p.created_at DESC")
    List<PostVO> findByUserId(Long userId);
    
    @Select("SELECT p.*, u.username FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "WHERE p.deleted = 0 ORDER BY p.created_at DESC")
    List<PostVO> findAll();
    
    @Select("SELECT p.*, u.username FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "WHERE p.title LIKE CONCAT('%', #{title}, '%') AND p.deleted = 0 ORDER BY p.created_at DESC")
    List<PostVO> findByTitleLike(@Param("title") String title);
    
    @Select("SELECT p.*, u.username FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "WHERE p.content LIKE CONCAT('%', #{content}, '%') AND p.deleted = 0 ORDER BY p.created_at DESC")
    List<PostVO> findByContentLike(@Param("content") String content);
    
    @Select("SELECT p.*, u.username FROM post p " +
            "LEFT JOIN user u ON p.user_id = u.id " +
            "WHERE u.username LIKE CONCAT('%', #{author}, '%') AND p.deleted = 0 " +
            "ORDER BY p.created_at DESC")
    List<PostVO> findByAuthorLike(@Param("author") String author);
} 